"""
测试方法流程：
    1. 实例化session对象
    2. session对象发请求
    3. 断言
    4. 关闭session

* 获取验证码： http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify
* 登录： http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login
"""

import unittest

import requests
from htmltestreport import HTMLTestReport


class TestTpshop(unittest.TestCase):
    def test_login_ok(self):
        session=requests.Session()
        url='http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify'
        response=session.get(url)
        print(response.headers.get('Content-Type'))
        self.assertIn('image',response.headers.get('Content-Type'))
        url='http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login'
        info={
            'username': '13344445555',
            'password': '123456',
            'verify_code': '8888'
        }
        response=session.post(url,data=info)
        print(response.json())
        self.assertEqual(200,response.status_code)
        self.assertEqual(1,response.json().get('status'))
        self.assertIn('成功',response.json().get('msg'))
        session.close()

    def test_login_user_exist(self):
            session = requests.Session()
            url = 'http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify'
            response = session.get(url)
            print(response.headers.get('Content-Type'))
            self.assertIn('image', response.headers.get('Content-Type'))
            url = 'http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login'
            info = {
                'username': '13344449897111',
                'password': '123456',
                'verify_code': '8888'
            }
            response = session.post(url, data=info)
            print(response.json())
            self.assertEqual(200, response.status_code)
            self.assertEqual(-1, response.json().get('status'))
            self.assertIn('不存在', response.json().get('msg'))
            session.close()

    def test_login_pwd_err(self):
        session=requests.Session()
        url='http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify'
        response=session.get(url)
        print(response.headers.get('Content-Type'))
        self.assertIn('image',response.headers.get('Content-Type'))
        url='http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login'
        info={
            'username': '13344445555',
            'password': '123456abc',
            'verify_code': '8888'
        }
        response=session.post(url,data=info)
        print(response.json())
        self.assertEqual(200,response.status_code)
        self.assertEqual(-2,response.json().get('status'))
        self.assertIn('错误',response.json().get('msg'))
        session.close()
if __name__ == '__main__':
    suite = unittest.defaultTestLoader.discover('./', 'hm11*')
    report = HTMLTestReport('./tpshop登录测试报告.html', title="测试报告", description="V1.0")
    report.run(suite)




